package com.mine.distinctionteam.ui.home;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mine.distinctionteam.R;
import com.mine.distinctionteam.bean.home.ContentsBean;
import com.mine.distinctionteam.bean.home.HomeBean;
import com.mine.distinctionteam.bean.home.HomeDatasBean;
import com.mine.distinctionteam.ui.home.adapter.AdvertisementViewPagerAdapter;
import com.mine.distinctionteam.ui.home.adapter.ItemListViewAdapter;
import com.mine.distinctionteam.ui.home.adapter.RecommendTodayRecyclerViewAdapter;
import com.mine.distinctionteam.ui.home.adapter.TimeActivityViewPagerAdapter;
import com.mine.distinctionteam.ui.myclass.ClassActivity;
import com.mine.distinctionteam.widget.MyListView;
import com.mine.distinctionteam.widget.VerticalScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.mine.distinctionteam.R.id.imageView_time;
import static com.mine.distinctionteam.R.id.listView_fragment_home;
import static com.mine.distinctionteam.R.id.recyclerView_fragment_home_today;
import static com.mine.distinctionteam.R.id.textView_fragment_today_heart;
import static com.mine.distinctionteam.R.id.textView_fragment_today_time;

/**
 *
 */
public class HomeFragment extends Fragment implements HomeContract.View {
    @BindView(R.id.imageView_fragment_home_menu)
    ImageView imageViewFragmentHomeMenu;
    @BindView(R.id.linearLayout_fragment_home_search)
    LinearLayout linearLayoutFragmentHomeSearch;
    @BindView(R.id.viewPager_fragment_home)
    ViewPager viewPagerFragmentHome;
    @BindView(R.id.imageView_fragment_home_new)
    ImageView imageViewFragmentHomeNew;
    @BindView(R.id.textView_fragment_home_new)
    TextView textViewFragmentHomeNew;
    @BindView(R.id.linearLayout_fragment_home_new)
    LinearLayout linearLayoutFragmentHomeNew;
    @BindView(R.id.imageView_fragment_home_sell)
    ImageView imageViewFragmentHomeSell;
    @BindView(R.id.textView_fragment_home_sell)
    TextView textViewFragmentHomeSell;
    @BindView(R.id.linearLayout_fragment_home_sell)
    LinearLayout linearLayoutFragmentHomeSell;
    @BindView(R.id.imageView_fragment_home_store)
    ImageView imageViewFragmentHomeStore;
    @BindView(R.id.textView_fragment_home_store)
    TextView textViewFragmentHomeStore;
    @BindView(R.id.linearLayout_fragment_home_store)
    LinearLayout linearLayoutFragmentHomeStore;
    @BindView(R.id.imageView_fragment_home_discount)
    ImageView imageViewFragmentHomeDiscount;
    @BindView(R.id.textView_fragment_home_discount)
    TextView textViewFragmentHomeDiscount;
    @BindView(R.id.linearLayout_fragment_home_discount)
    LinearLayout linearLayoutFragmentHomeDiscount;
    @BindView(R.id.imageView_heart)
    ImageView imageViewHeart;
    @BindView(textView_fragment_today_heart)
    TextView textViewFragmentTodayHeart;
    @BindView(R.id.imageView_fragment_home_left)
    ImageView imageViewFragmentHomeLeft;
    @BindView(recyclerView_fragment_home_today)
    RecyclerView recyclerViewFragmentHomeToday;
    @BindView(R.id.imageView_fragment_home_right)
    ImageView imageViewFragmentHomeRight;
    @BindView(imageView_time)
    ImageView imageViewTime;
    @BindView(textView_fragment_today_time)
    TextView textViewFragmentTodayTime;
    @BindView(R.id.viewPager_fragment_home_timeActivity)
    ViewPager viewPagerFragmentHomeTimeActivity;
    @BindView(listView_fragment_home)
    MyListView listViewFragmentHome;
    @BindView(R.id.linearLayout_fragment_home_weiXin)
    LinearLayout linearLayoutFragmentHomeWeiXin;
    @BindView(R.id.linearLayout_fragment_home_tel)
    LinearLayout linearLayoutFragmentHomeTel;
    @BindView(R.id.pullToRefreshScrollView_fragment_home)
    VerticalScrollView pullToRefreshScrollViewFragmentHome;
    private HomePresenter presenter;
    private Context mContext;
    //广告栏适配器 viewPager
    private AdvertisementViewPagerAdapter advertisementViewPagerAdapter;
    //今日推荐适配器
    private RecommendTodayRecyclerViewAdapter recommendTodayRecyclerViewAdapter;
    //限时活动 适配器
    private TimeActivityViewPagerAdapter timeActivityViewPagerAdapter;
    //物品的listView
    private ItemListViewAdapter itemListViewAdapter;
    private List<ContentsBean> contentsBeanList=new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=getContext();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        presenter=new HomePresenter(this);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //将今日推荐的recyclerView设置成横向滚动的
        LinearLayoutManager manager=new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewFragmentHomeToday.setLayoutManager(manager);

        presenter.getHome();

    }

    @Override
    public void getHomeSuccess(final HomeBean homeBean) {
        //最上面的广告栏 viewPager
        List<ContentsBean> contentsList = homeBean.getHomeDatas().get(0).getContents();
        advertisementViewPagerAdapter=new AdvertisementViewPagerAdapter(mContext,contentsList);
        viewPagerFragmentHome.setAdapter(advertisementViewPagerAdapter);
        //最近上新
        Glide.with(mContext).load(homeBean.getHomeDatas().get(1).getContents().get(0).getImage()).into(imageViewFragmentHomeNew);
        imageViewFragmentHomeNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, ClassActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("myClass",homeBean.getHomeDatas().get(1).getContents().get(0).getAction().getAlias());
                bundle.putString("myClassTitle",homeBean.getHomeDatas().get(1).getContents().get(0).getAction().getTitle());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        textViewFragmentHomeNew.setText(homeBean.getHomeDatas().get(1).getContents().get(0).getAction().getTitle());
        Glide.with(mContext).load(homeBean.getHomeDatas().get(1).getContents().get(1).getImage()).into(imageViewFragmentHomeSell);
        imageViewFragmentHomeSell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, ClassActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("myClass",homeBean.getHomeDatas().get(1).getContents().get(1).getAction().getAlias());
                bundle.putString("myClassTitle",homeBean.getHomeDatas().get(1).getContents().get(1).getAction().getTitle());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        textViewFragmentHomeSell.setText(homeBean.getHomeDatas().get(1).getContents().get(1).getAction().getTitle());
        Glide.with(mContext).load(homeBean.getHomeDatas().get(1).getContents().get(2).getImage()).into(imageViewFragmentHomeStore);
        imageViewFragmentHomeStore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, ClassActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("myClass",homeBean.getHomeDatas().get(1).getContents().get(2).getAction().getAlias());
                bundle.putString("myClassTitle",homeBean.getHomeDatas().get(1).getContents().get(2).getAction().getTitle());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        textViewFragmentHomeStore.setText(homeBean.getHomeDatas().get(1).getContents().get(2).getAction().getTitle());
        Glide.with(mContext).load(homeBean.getHomeDatas().get(1).getContents().get(3).getImage()).into(imageViewFragmentHomeDiscount);
        imageViewFragmentHomeDiscount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, ClassActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("myClass",homeBean.getHomeDatas().get(1).getContents().get(3).getAction().getAlias());
                bundle.putString("myClassTitle",homeBean.getHomeDatas().get(1).getContents().get(3).getAction().getTitle());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        textViewFragmentHomeDiscount.setText(homeBean.getHomeDatas().get(1).getContents().get(3).getAction().getTitle());
        //今日推荐   因为json解析出来的数据名字都是一样的  需要单独判断
        contentsBeanList=homeBean.getHomeDatas().get(2).getContents();
        recommendTodayRecyclerViewAdapter=new RecommendTodayRecyclerViewAdapter(mContext,contentsBeanList);
        recyclerViewFragmentHomeToday.setAdapter(recommendTodayRecyclerViewAdapter);


        //今日推荐 名字和图标
        Glide.with(mContext).load(homeBean.getHomeDatas().get(2).getIcon()).into(imageViewHeart);
        textViewFragmentTodayHeart.setText(homeBean.getHomeDatas().get(2).getTitle());
        //限时活动
        List<ContentsBean>timeList=homeBean.getHomeDatas().get(3).getContents();
        timeActivityViewPagerAdapter=new TimeActivityViewPagerAdapter(mContext,timeList);
        viewPagerFragmentHomeTimeActivity.setAdapter(timeActivityViewPagerAdapter);
        textViewFragmentTodayTime.setText(homeBean.getHomeDatas().get(3).getTitle());
        Glide.with(mContext).load(homeBean.getHomeDatas().get(3).getIcon()).into(imageViewTime);
        //listView
        List<HomeDatasBean> homeDatasBeanList=homeBean.getHomeDatas().subList(4,homeBean.getHomeDatas().size());
        itemListViewAdapter=new ItemListViewAdapter(mContext,homeDatasBeanList);
        listViewFragmentHome.setAdapter(itemListViewAdapter);
    }
    @Override
    public void getHomeFail(String erro) {


    }
}
